package com.southwind.springboottest.study.Java中通讯.http协议;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpClientExample {

    public static void main(String[] args) throws Exception {
        // 创建一个 URL 对象，指向服务器地址
        URL url = new URL("http://localhost:8080/");

        // 打开 HTTP 连接
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET"); // 设置请求方法为 GET

        // 从连接中获取输入流，并读取响应内容
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        // 打印响应内容
        System.out.println("HTTP Response: " + response.toString());
    }
}

